

//
//  TravelTourImageCollectionCell.m
//  ToWhere
//
//  Created by dllo on 15/11/23.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "TravelTourImageCollectionCell.h"
#import <AVFoundation/AVFoundation.h>

@interface TravelTourImageCollectionCell ()

@property (nonatomic, retain) AVPlayer *player;
@property (nonatomic, retain) UIButton *btn;
@property (nonatomic, retain) UIView *vw;

@end

@implementation TravelTourImageCollectionCell
-(void)dealloc{
    [_model release];
    [_btn release];
    [_player release];
    [_vw release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createView];
    }
    
    return self;
}

-(void)createView
{
    self.vw = [[UIView alloc]init];
    
    [self addSubview:self.vw];
    [_vw release];
    
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.backgroundColor = [UIColor clearColor];
    [self.btn setImage:[UIImage imageNamed:@"bofang"] forState:UIControlStateNormal];
   
    
    [self.btn addTarget:self action:@selector(playOrPauseAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
}


-(void)setModel:(TravelTourSmallModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    self.vw.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:self.model.video_url]];
    
    AVPlayerLayer *lay = [AVPlayerLayer playerLayerWithPlayer:_player];
    
    lay.frame = self.vw.frame;
    [self.vw.layer addSublayer:lay];
    
     [self.vw addSubview:self.btn];
   
    self.btn.frame = CGRectMake(0, 0, self.vw.frame.size.width, self.vw.frame.size.height);
    
    
//    [self.player play];
    [_player release];

}

-(void)playOrPauseAction:(UIButton *)sender
{
    sender.selected = !sender.isSelected;
    
    if (sender.selected) {
       // [_player play];
        [sender setImage:NULL forState:UIControlStateNormal];
        
        [self.delegate string:self.model.video_url];
        
        
    }else
    {
        [sender setImage:[UIImage imageNamed:@"bofang"] forState:UIControlStateNormal];
       // [_player pause];
    }
    
    
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
